$(document).ready(function () {

    var counterQuestions = 2;
    var counterAnswers = 1;
    var extraCounterAnswers = 1;
    var keepTrack = new Array();

    $("#addQuestion").click(function () {

        counterAnswers = 1;
        var newTextBoxDiv = $(document.createElement('ul')).attr("id", 'QuestionDiv' + counterQuestions);
        newTextBoxDiv.html('<label>Question #' + counterQuestions + ' : </label>' +
				'<input type="text" name="textbox' + counterQuestions +
				'" id="textbox' + counterQuestions + '" value="" >');

        newTextBoxDiv.appendTo("#QuestionGroup");

        counterQuestions++;
    });

    $("#removeQuestion").click(function () {
        if (counterQuestions == 1) {
            alert("No more Questions to remove");
            return false;
        }
        counterQuestions--;

        $("#QuestionDiv" + counterQuestions).remove();
    });

    $("#addAnswer").click(function () {

        var newAnswerBoxDiv = $(document.createElement('li')).attr("id", 'AnswerDiv' + counterAnswers);
        newAnswerBoxDiv.html('<label>Answer #' + counterAnswers + ' : </label>' +
				'<input type="text" name="answerNamebox' + counterAnswers +
				'" id="answerBox' + counterAnswers + 'questionNr' + counterQuestions + '" value="" >' +
				'<input type="radio" name="radioButton' + counterAnswers + '" value = True""/>'
				+ 'True? <br />');

        newAnswerBoxDiv.appendTo("#QuestionGroup");
        counterAnswers++;
        keepTrack[counterQuestions] = counterAnswers;
        extraCounterAnswers++;
    });

    $("#removeAnswer").click(function () {
        if (counterAnswers == 1) {
            alert("No more Answers to remove");
            return false;
        }
        counterAnswers--;

        $("#AnswerDiv" + counterAnswers).remove();
    });

    $("#getQuestionValue").click(function () {

        var msg = '';
        for (i = 1; i < counterQuestions; i++) {
            msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
        }
        alert(msg);
    });

    $("#getAnswerValue").click(function () {

        var msg2 = '';
        for (q = 2; q <= counterQuestions; q++) {
            msg2 += "\nQuestion #" + q;
            for (j = 1; j < keepTrack[q]; j++) {
                msg2 += "\n Answer #" + j + " : " + $('#answerBox' + j + 'questionNr' + q).val();
            }
        }
        alert(msg2);
        document.getElementById("Hidden1").value = msg2;
    });

});
